﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace ProCP.Sim
{
    [Serializable()]
    class Lane
    {
        //ToDraw

        //logic
        int width;
        int[] statistics;
        protected Point[] laneCurve;
        public List<Car> carsOnLane;
        Queue<Car> genratedCars = new Queue<Car>();
        public Lane(int width, Point entery, Point exit )
        {
            this.width = width;
            carsOnLane = new List<Car>();
            laneCurve = new Point[2];

            laneCurve[0] = entery;
            laneCurve[1] = exit; 
        }

        public bool AddCar(int[] destination)
        {
            if (CarAHead()) return false;
            carsOnLane.Add(new Car(laneCurve[0], new Size(2 * width / 3, 2 * width / 3), destination));
            return true;
            //genratedCars.Enqueue(new Car(laneCurve[0], new Size(2 * width / 3, 2 * width / 3), destination));
            //if (carsOnLane.Count < 13)
            //{
            //        carsOnLane.Add(genratedCars.Dequeue());
            //}
        }

        public bool AddCar(Car c)
        {
            if (CarAHead())
                return false;
            c.stage = 0;
            carsOnLane.Add(c);
            return true;
            //genratedCars.Enqueue(c);
            //if (carsOnLane.Count < 13)
            //{
            //    if (genratedCars.Count > 0)
            //    {
            //        carsOnLane.Add(genratedCars.Dequeue());
            //        return true;
            //    }
            //}
            
        }

        public bool MoveCars()
        {
            int index = carsOnLane.Count - 1; 
            while (index>=0)
            {
                Car c = carsOnLane[index];
                if (c != null)
                {
                    if (!CarAHead(index))
                    {
                        if (c.stage < 1)
                        {//combining to one if doesnt work for some reason...strange
                            if (c.MoveTo(laneCurve[0]))
                            {
                                c.stage++;

                            }
                        }
                        else if (c.MoveTo(laneCurve[1]))
                        {

                            return true;
                        }
                    }
                }
                index--;
            }
            return false;
        }

        private bool CarAHead(int index)
        {
            if (index > 0)
            {
                Car c = carsOnLane[index];
                if (c != null)
                {
                    Car carInFront = carsOnLane[index - 1];
                    if (carInFront != null)
                    {
                        if (c.Buffer.IntersectsWith(carInFront.ToDraw))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        private bool CarAHead()//if can add extra car
        {
            if (carsOnLane.Count!=0)
            {
                Car lastCar = carsOnLane[carsOnLane.Count - 1];
                RectangleF newCarArea = new RectangleF(laneCurve[0], lastCar.ToDraw.Size);
                if (newCarArea.IntersectsWith(lastCar.ToDraw))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
